
local PoisonZombie = {}
PoisonZombie[PLAYER_IDLE] = ACT_IDLE
PoisonZombie[PLAYER_WALK] = ACT_WALK
PoisonZombie[PLAYER_JUMP] = ACT_WALK
PoisonZombie[PLAYER_ATTACK1] = ACT_MELEE_ATTACK1
PoisonZombie[PLAYER_SUPERJUMP] = ACT_RANGE_ATTACK2

function PoisonZombieAnim(ply, anim)
	local act = ACT_IDLE
	local speed = ply:GetVelocity():Length()

	if PoisonZombie[anim] != nil then
		act = PoisonZombie[anim]
	else
		if speed > 0 then
			act = ACT_WALK
		end
	end

	if act == ACT_IDLE_ON_FIRE then
		if speed > 0 then
			act = ACT_WALK_ON_FIRE
		end
	end

	if act == ACT_MELEE_ATTACK1 or anim == PLAYER_SUPERJUMP then
		ply:SetPlaybackRate(2)
		ply:RestartGesture(act)
		return true
	end

	local seq = ply:SelectWeightedSequence(act)
	if act == ACT_WALK then
		seq = 3
	end

	if seq == 3 then
		ply:SetPlaybackRate(1.5) //walk
	else
		ply:SetPlaybackRate(1.0)
	end

	if ply:GetSequence() == seq then return true end
	ply:ResetSequence(seq)
	ply:SetCycle(0)
	return true
end

local FastZombie = {}
FastZombie[PLAYER_IDLE] = ACT_IDLE
FastZombie[PLAYER_WALK] = ACT_RUN
FastZombie[PLAYER_ATTACK1] = ACT_MELEE_ATTACK1
FastZombie[PLAYER_SUPERJUMP] = ACT_CLIMB_UP

function FastZombieAnim(ply, anim)
	local act = ACT_IDLE
	local speed = ply:GetVelocity():Length()
	local ground = ply:OnGround()

	if FastZombie[anim] != nil then
		act = FastZombie[anim]
	else
		if speed > 0 then
			act = ACT_RUN
		end
	end

	if act == ACT_MELEE_ATTACK1 or act == ACT_CLIMB_UP then
		ply:RestartGesture(act)
		return true
	end

	local seq = ply:SelectWeightedSequence(act)

	if not ground and act != ACT_CLIMB_UP then
	    seq = 3
	end

	if ply:GetSequence() == seq then return true end
	ply:ResetSequence(seq)
	ply:SetPlaybackRate(1.0)
	ply:SetCycle(0)
	return true
end

// Classic Zombie
local Zombie = {}
Zombie[PLAYER_IDLE] = ACT_IDLE
Zombie[PLAYER_WALK] = ACT_WALK
Zombie[PLAYER_JUMP] = ACT_WALK
Zombie[PLAYER_ATTACK1] = ACT_MELEE_ATTACK1
Zombie[PLAYER_SUPERJUMP] = ACT_IDLE_ON_FIRE

function ZombieAnim(ply, anim)
	local act = ACT_IDLE
	local speed = ply:GetVelocity():Length()

	if Zombie[anim] != nil then
		act = Zombie[anim]
	else
		if speed > 0 then
			act = ACT_WALK
		end
	end

	if act == ACT_IDLE_ON_FIRE then
		if speed > 0 then
			act = ACT_WALK_ON_FIRE
		end
	end

	if act == ACT_MELEE_ATTACK1 or anim == PLAYER_SUPERJUMP then
		ply:SetPlaybackRate(2)
		ply:RestartGesture(act)
		return true
	end

	local seq = ply:SelectWeightedSequence(act)
	if act == ACT_WALK then
		seq = 2
	end

	if seq == 2 then
		ply:SetPlaybackRate(1.5)
	else
		ply:SetPlaybackRate(1.0)
	end
	if ply:GetSequence() == seq then return true end
	ply:ResetSequence(seq)
	ply:SetCycle(0)
	return true
end

local ATT = {}
ATT[PLAYER_RELOAD] = ACT_HL2MP_GESTURE_RELOAD
ATT[PLAYER_JUMP] = ACT_HL2MP_JUMP
ATT[PLAYER_ATTACK1] = ACT_HL2MP_GESTURE_RANGE_ATTACK

function GM:SetPlayerAnimation(ply, anim)
	if ply:Team() == TEAM_UNDEAD then
		if SpecialAnims[ply:GetModel()] then
			SpecialAnims[ply:GetModel()](ply, anim)
			return
		end
	end

	local act = ACT_HL2MP_IDLE
	local Speed = ply:GetVelocity():Length()
	local OnGround = ply:OnGround()

	if ATT[anim] ~= nil then
		act = ATT[anim]
	else
		if OnGround and ply:Crouching() then
			act = ACT_HL2MP_IDLE_CROUCH
			if Speed > 0 then
				act = ACT_HL2MP_WALK_CROUCH
			end
		elseif Speed > 0 then
			act = ACT_HL2MP_RUN
		end
	end

	if act == ACT_HL2MP_GESTURE_RANGE_ATTACK or act == ACT_HL2MP_GESTURE_RELOAD then
		ply:RestartGesture(ply:Weapon_TranslateActivity(act))
		if act == ACT_HL2MP_GESTURE_RANGE_ATTACK then
			ply:Weapon_SetActivity(ply:Weapon_TranslateActivity(ACT_RANGE_ATTACK1), 0)
		end
		return
	end

	if not OnGround then
		act = ACT_HL2MP_JUMP
	end

	local seq = ply:SelectWeightedSequence(ply:Weapon_TranslateActivity(act))

	// Let's assume that no admins spawn any vehicles and that we always have a weapon.

	if ply:GetSequence() == seq then return end
	ply:SetPlaybackRate(1.0)
	ply:ResetSequence(seq)
	ply:SetCycle(0)
end

SpecialAnims = {}
SpecialAnims["models/player/corpse1.mdl"] = nil
SpecialAnims["models/zombie/fast.mdl"] = FastZombieAnim
SpecialAnims["models/zombie/poison.mdl"] = PoisonZombieAnim
SpecialAnims["models/player/charple01.mdl"] = nil
SpecialAnims["models/zombie/classic.mdl"] = ZombieAnim
